package com.mine.crawler.main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.mine.crawler.config.InitializeCrawlerApplication;
import com.mine.crawler.util.urlLink;

public class CrawlerThread implements Runnable
{
	private Logger log = Logger.getLogger(CrawlerThread.class);
	Properties InitConfig = InitializeCrawlerApplication.getInitConfig();

	private String[] strArr;
	// Why linkMap is repeated in CrawlerData and CrawlerThread?
	private Map<Integer, String> linkMap;
	private Map<Integer, urlLink> linkMapUrl;
	private List<String> httpPatternList= new ArrayList<String>();

	private String ptnHttp = "http:.+?(\"|\\s)";
	private String ptnHref = "href(\\s*?)=(\\s*?).+?(\")" ;
	private String ptnSrc = "(href|src)(\\s*?)=(\\s*?).+?(\")";
	
	private String urlCleaned;
	private int SIZE_PER_LINE = 
		Integer.parseInt((String)InitConfig.getProperty("URL_listSize_perHTML_line")) ;
	
	public CrawlerThread()
	{
		httpPatternList.add(ptnHttp);
		httpPatternList.add(ptnHref);
		httpPatternList.add(ptnSrc);
	}
	@Override
	public void run()
	{
		// TODO Auto-generated method stub	
	}
	
	/**
	* Extracts URL from the passed line and presents all urls in a map
	*
	* @param line 
	* @param base -TOCHECK; should be the domain of HTTP-URL
	* @return Map 
	*/
	public Map<Integer, String> extractURL(String line, String base)
	{
		strArr = new String[SIZE_PER_LINE];
		linkMap = new HashMap<Integer, String>(SIZE_PER_LINE);
		linkMapUrl = new HashMap<Integer, urlLink>(SIZE_PER_LINE);
		urlLink urlToInsert;
		Iterator<String> it=httpPatternList.iterator();
		
		while(it.hasNext())
		{
			strArr = matchPattern((String)it.next(), line, base, 1);
		
			for(int strArrLength = strArr.length-1; strArrLength >= 0; strArrLength--)
			{
				//urlCleaned = CleanedURL(strArr[strArrLength], base, 1) ;
				//System.out.println("Length "+strArrLength);
				if(strArr[strArrLength] != null)
				{
					linkMap.put(new Integer(strArr[strArrLength].hashCode()), strArr[strArrLength]);
					
					urlToInsert = new urlLink(strArr[strArrLength]);	
					linkMapUrl.put(urlToInsert.hashCode(), 
							urlToInsert );
				}
			}
			
			//strArr = matchPattern(ptnSrc, line);
			
			//linkMap.put(new Integer(matcher.group().hashCode()), matcher.group()); 
	        //System.out.println("No of elements found: "+linkMap.size());
		}
		return linkMap;	
	}
	
	/**
	 * Match strPattern in the given line 
	 * 
	 * @param strPattern
	 * @param line
	 * @param base
	 * @param type
	 * @return string array containing all the cleaned URLs 
	 */
	protected String[] matchPattern(String strPattern, String line, String base, int type)
	{
		String[] strArr_local = new String[SIZE_PER_LINE];
		Pattern pattern = Pattern.compile(strPattern);
		Matcher matcher = pattern.matcher(line); // String 
		String logMsg;
		
        boolean DEBUG_MATCH_PATTERN = false;

        int i_loop = 0;
        while (matcher.find()) 
        {
        	urlCleaned = CleanedURL(matcher.group(), base, 1);
        	
        	logMsg = "I found the text |"+urlCleaned+"| starting at " +
            	"index "+matcher.start()+" and ending at index "
            	+matcher.end()+".%n";
        	log.debug(logMsg);
            
            strArr_local[i_loop] = urlCleaned;
            
            DEBUG_MATCH_PATTERN = true;
            i_loop = i_loop+1;
        }

        if(!DEBUG_MATCH_PATTERN)
        {
            System.out.println("No match found.");
        }

        return strArr;
	}
	
	/**
	 * Cleans URL passed on the type
	 *  e.g. for type 1 it removes the last character from the String
	 * @param toClean
	 * @param base
	 * @param type
	 * @return
	 */
	protected static String CleanedURL(String toClean, String base, int type)
	{		
		String result = null;
		if(type == 1)
			result = toClean.substring(0, toClean.length()-1);
		
		return result;
	}

}
